package com.aigao.golf.common.permissions

/**
 * 默认角色权限
 */
object DefaultRoles {
    private val roles = arrayOf(
            Role("我的信息", PermissionType.MY_INFO.ordinal),
            Role("机构管理", PermissionType.GROUP_USER.ordinal,
                    PermissionType.GROUP_INFO.ordinal, PermissionType.GROUP_USER_PRIVILEGE.ordinal),
            Role("机构用户", PermissionType.GROUP_USER.ordinal or (PermissOpt.UPDATE.code shl 8))
    )

    /**
     * 角色名称转授权码
     */
    fun roleToString(name: String): String? {
        var role: Role? = roles.find { r -> r.name == name }
        role?.let {
            var map = it.permissions.associateBy(
                    { if (it > 255) PermissionType.values()[it and 0xFF] else PermissionType.values()[it] },
                    { if (it > 255) (it ushr 8).toString(16) else 'F' }).toMap()
            return PermissionType.values()
                    .map { p -> if (map.contains(p)) map[p] else '0' }
                    .joinToString(separator = "")
        }
        return null
    }
}